﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;

namespace WebSys.UI.OfficeFlow
{
    public class DoCancelSuspendSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        DropDownList ddlWname;
        Button btnOK, btnSuspend;
        CustomGridView gv;
        Label labMsg, lbresult;
        TextBox txtIId, txtName;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitControl();
            if (!base.Page.IsPostBack)
            {
                BindDDL();
                BindGridView();
            }
            gv.OnLoadData += new EventHandler(gv_OnLoadData);
        }

        protected void gv_OnLoadData(object sender, EventArgs e)
        {
            BindGridView();
        }

        private void InitControl()
        {
            ddlWname = (DropDownList)FindControl("ddlWname");
            btnOK = (Button)FindControl("btnOK");
            btnSuspend = (Button)FindControl("btnSuspend");
            gv = (CustomGridView)FindControl("gv");
            labMsg = (Label)FindControl("labMsg");
            lbresult = (Label)FindControl("lbresult");
            txtIId = (TextBox)FindControl("txtIId");
            txtName = (TextBox)FindControl("txtName");
            btnOK.Click += new EventHandler(btnOK_Click);
            btnSuspend.Click += new EventHandler(btnSuspend_Click);
        }

        /// <summary>
        /// 绑定数据到GridView上
        /// </summary>
        private void BindGridView()
        {
            labMsg.Text = "";
            DataTable dt = _FlowFactoryProvider.GetSuspendLists(txtIId.Text.Trim(), txtName.Text.Trim(), ddlWname.SelectedValue, UserId);
            if (dt.Rows.Count > 0)
            {
                lbresult.Visible = false;
                gv.Visible = true;
                gv.DataSource = dt;
                gv.RecordCount = dt.Rows.Count;
                gv.DataBind();
            }
            else
            {
                lbresult.Visible = true;
                gv.Visible = false;
                lbresult.Text = "没有可显示数据！";
            }
        }

        private void BindDDL()
        {
            DataTable dt = _FlowFactoryProvider.GetSuspendWname(UserId);
            ddlWname.DataSource = dt;
            ddlWname.DataTextField = "wname";
            ddlWname.DataValueField = "wname";
            ddlWname.DataBind();

            ListItem item = new ListItem();
            item.Text = dt.Rows.Count == 0 ? "——无挂起流程——" : "——所有流程——";
            item.Value = "";
            ddlWname.Items.Insert(0, item);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            BindGridView();
            labMsg.Text = "";
        }

        protected void btnSuspend_Click(object sender, EventArgs e)
        {
            string iid = "";
            string uid = UserId;
            foreach (GridViewRow row in gv.Rows)
            {
                CheckBox cb = ((CheckBox)row.Cells[0].FindControl("cb"));
                if (cb.Checked == true)
                {
                    iid += "," + row.Cells[1].Text;
                }
            }
            iid = iid.Substring(1);
            string[] iidArray = iid.Split(',');
            string tempiid = "";
            int j = 0;
            for (int i = 0; i < iidArray.Length; i++)
            {
                if (_FlowFactoryProvider.GetSuUser(iidArray[0]) != uid)
                {
                    j++;
                    tempiid = iidArray[0];
                    break;
                }
            }
            if (j != 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('您不能取消挂起业务编号为：" + tempiid + "的案子');", true);
                return;
            }
            for (int i = 0; i < iidArray.Length; i++)
            {
                bool result = _FlowFactoryProvider.CancelSuspend(iidArray[i].ToString(), UserId);
                if (!result)
                    throw new Exception("取消挂起过程出错");
            }

            BindGridView();
            labMsg.Text = "操作成功！";
        }
    }
}


